package me.leiusual.sharing.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.load.resource.bitmap.FitCenter
import dagger.android.support.DaggerFragment
import kotlinx.android.synthetic.main.fragment_home_home.*
import kotlinx.android.synthetic.main.fragment_home_home.view.*
import me.leiusual.sharing.R
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.helper.HandleObserver
import me.leiusual.sharing.ui.home.adapter.HomeHomeAdapter
import me.leiusual.sharing.util.extension.setImageUrl
import timber.log.Timber
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/15
 */
class HomeHomeFragment : DaggerFragment(), View.OnClickListener {

  @Inject
  lateinit var factory: ViewModelProvider.Factory

  private val viewModel: HomeHomeViewModel by viewModels { factory }

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_home_home, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    toolbar.setOnMenuItemClickListener {
      Timber.v("menuItem = %s, itemId = %s, groupId = %s", it, it.itemId, it.groupId)
      if (it.groupId == 1) {
        viewModel.storeIndex(viewModel.store.bindingStoreId, it.itemId.toLong())
        true
      } else {
        false
      }
    }

    // 搜索框
    edit_text_search.setOnClickListener(this)

    // 更新
    updateViews()

    recycler_view.adapter = HomeHomeAdapter()

    observeData()
    viewModel.storeIndex(viewModel.store.bindingStoreId, viewModel.store.currentStoreId)
  }

  override fun onClick(v: View?) {
    when (v?.id) {
      R.id.edit_text_search -> {
        findNavController().navigate(R.id.action_homeHomeFragment_to_homeSearchFragment)
      }
    }
  }

  private fun updateViews() {

    // 更新可切换按钮
    toolbar.menu.clear()
    val subMenu = toolbar.menu.addSubMenu("切换")
    subMenu.item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
    viewModel.store.storeList.forEach { entry ->
      subMenu.add(1, entry.key.toInt(), 0, entry.value)
    }
  }

  private fun observeData() {
    viewModel.storeIndex.observe(viewLifecycleOwner, HandleObserver {
      Timber.v("storeIndex = %s", it)
      when (it) {
        is Data.Loading -> {
          state_constraint_layout.showProgress()
        }
        is Data.Error -> {
          state_constraint_layout.showError(it.error.message)
        }
        is Data.Success -> {
          state_constraint_layout.showContent()

          // 更新当前店铺ID
          viewModel.store.currentStoreId = it.data.store.id

          // 更新LOGO
          toolbar.image_view_logo.setImageUrl(it.data.store.logoUrl) { request -> request.transform(FitCenter()) }

          // 更新商品列表
          val adapter = recycler_view.adapter as HomeHomeAdapter
          adapter.submitList(it.data)
        }
      }
    })
  }
}